﻿using BasicInformation.Domain;

using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI.Common;
using SCM.API.Read.Application.Command.Purchase;
using SCM.ErrorCode;
using SCM.Infrastructure.Implement;
using SqlSugar;


namespace SCM.API.Read.Application.Handler.Purchase
{
    public class GetVendorHandler : IRequestHandler<GetVendorCommand, APIResult<SupplierModel>>
    {
        DatabaseContext db;

        public GetVendorHandler(DatabaseContext db)
        {
            this.db = db;
        }

        public async Task<APIResult<SupplierModel>> Handle(GetVendorCommand request, CancellationToken cancellationToken)
        {
            var result = new APIResult<SupplierModel>();
            var supplierName =  db.SqlSugar.Queryable<SupplierModel>().Where(s => s.SupplierId == request.SupplierId && s.IsDel == false);

            if (supplierName==null)
            {
                result.Code = APIEnum.Fail;
                result.Msg = "供应商不存在";
                return await Task.FromResult(result); 
            }
            
            result.Code = APIEnum.Success;
            result.Msg = "查询供应商成功";
            result.Datas = supplierName.ToList();
            return await Task.FromResult(result);
        }
    }
}
